{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PLEASE USE `Cell => Run All` ON THIS NOTEBOOK (MENU OPTION ABOVE).\n",
    "\n",
    "# THIS NOTEBOOK WILL TAKE 20-30 MINUTES TO COMPLETE SUCCESSFULLY.\n",
    "\n",
    "# PLEASE BE PATIENT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "\n",
    "print(\"Python Version %s\" % sys.version)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "!pip list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "%%bash\n",
    "conda list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. Install the CLI tools\n",
    "\n",
    "_Note:  Make sure the SageMaker role has access to create EKS Cluster._"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "\n",
    "source ~/.bash_profile\n",
    "\n",
    "pip install awscli --upgrade --user\n",
    "\n",
    "curl --silent --location \"https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz\" | tar xz -C /tmp\n",
    "\n",
    "sudo mv /tmp/eksctl /usr/local/bin\n",
    "\n",
    "eksctl version\n",
    "\n",
    "curl --location -o ./kubectl https://amazon-eks.s3.us-west-2.amazonaws.com/1.18.8/2020-09-18/bin/linux/amd64/kubectl\n",
    "#curl --location -o ./kubectl https://amazon-eks.s3.us-west-2.amazonaws.com/1.15.10/2020-02-22/bin/linux/amd64/kubectl\n",
    "\n",
    "chmod +x ./kubectl\n",
    "\n",
    "sudo mv ./kubectl /usr/local/bin\n",
    "\n",
    "kubectl version --short --client\n",
    "\n",
    "curl -o aws-iam-authenticator https://amazon-eks.s3.us-west-2.amazonaws.com/1.18.8/2020-09-18/bin/linux/amd64/aws-iam-authenticator\n",
    "#curl -o aws-iam-authenticator https://amazon-eks.s3.us-west-2.amazonaws.com/1.15.10/2020-02-22/bin/linux/amd64/aws-iam-authenticator\n",
    "\n",
    "chmod +x ./aws-iam-authenticator\n",
    "\n",
    "sudo mv aws-iam-authenticator /usr/local/bin\n",
    "\n",
    "aws-iam-authenticator version\n",
    "\n",
    "sudo yum -y install jq gettext\n",
    "\n",
    "for command in kubectl jq envsubst\n",
    "  do\n",
    "    which $command &>/dev/null && echo \"$command in path\" || echo \"$command NOT FOUND\"\n",
    "  done\n",
    "\n",
    "echo \"Completed\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. Configure the Environment Variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "\n",
    "source ~/.bash_profile\n",
    "\n",
    "export AWS_REGION=$(aws configure get region)\n",
    "echo \"export AWS_REGION=${AWS_REGION}\" | tee -a ~/.bash_profile\n",
    "\n",
    "export AWS_CLUSTER_NAME=workshop\n",
    "echo \"export AWS_CLUSTER_NAME=${AWS_CLUSTER_NAME}\" | tee -a ~/.bash_profile\n",
    "\n",
    "echo \"Completed\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. Create the EKS Cluster\n",
    "\n",
    "# THIS WILL TAKE 20-30 MINUTES.  PLEASE BE PATIENT!\n",
    "\n",
    "# _If you see `retryable error`s below, this is OK!_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "\n",
    "###############################\n",
    "#\n",
    "# ONLY RUN THIS CELL ONE TIME!!\n",
    "#\n",
    "###############################\n",
    "\n",
    "source ~/.bash_profile\n",
    "\n",
    "export AWS_CLUSTER_STATUS=$(aws eks describe-cluster --name ${AWS_CLUSTER_NAME} --region ${AWS_REGION} --query \"cluster.status\" --output text)\n",
    "\n",
    "if [ -n \"$AWS_CLUSTER_STATUS\" ]; then\n",
    "    if [ $AWS_CLUSTER_STATUS == \"ACTIVE\" ]; then\n",
    "        echo \"Cluster status:  $AWS_CLUSTER_STATUS.\"\n",
    "    else\n",
    "        echo \"Cluster status:  $AWS_CLUSTER_STATUS.  Please wait for status:  ACTIVE\"\n",
    "        exit\n",
    "    fi\n",
    "else\n",
    "    # THIS WILL TAKE 20-30 MINUTES.  PLEASE BE PATIENT. \n",
    "    eksctl create cluster \\\n",
    "      --name ${AWS_CLUSTER_NAME} \\\n",
    "      --version 1.18 \\\n",
    "      --nodes 4 \\\n",
    "      --node-type c5.xlarge \\\n",
    "      --node-volume-size 100 \\\n",
    "      --region=${AWS_REGION} \\\n",
    "      --zones=${AWS_REGION}a,${AWS_REGION}b \\\n",
    "      --alb-ingress-access\n",
    "      \n",
    "    export AWS_CLUSTER_STATUS=$(aws eks describe-cluster --name ${AWS_CLUSTER_NAME} --region ${AWS_REGION} --query \"cluster.status\" --output text)\n",
    "    echo \"export AWS_CLUSTER_STATUS=${AWS_CLUSTER_STATUS}\" | tee -a ~/.bash_profile\n",
    "    echo \"Cluster status:  $AWS_CLUSTER_STATUS\"\n",
    "fi"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# _If you see `retryable error`s  ^^ above ^^, this is OK!_\n",
    "\n",
    "# AFTER 20-30 MINUTES, YOUR CLUSTER WILL BE CREATED."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "\n",
    "source ~/.bash_profile\n",
    "\n",
    "export AWS_CLUSTER_STATUS=$(aws eks describe-cluster --name ${AWS_CLUSTER_NAME} --region ${AWS_REGION} --query \"cluster.status\" --output text)\n",
    "\n",
    "if [ -n \"$AWS_CLUSTER_STATUS\" ]; then\n",
    "    if [ $AWS_CLUSTER_STATUS == \"ACTIVE\" ]; then\n",
    "        echo \"Cluster status:  $AWS_CLUSTER_STATUS.  Please continue.\"\n",
    "    else\n",
    "        echo \"Cluster status:  $AWS_CLUSTER_STATUS.  Please wait for status:  ACTIVE\"\n",
    "        exit\n",
    "    fi\n",
    "fi"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "conda_python3",
   "language": "python",
   "name": "conda_python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
